﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataAccessLibrary;

namespace BusinessLayer
{
    public class ClsLogin
    {
        public string LoginID { get; set; }
        public int EmployeeID { get; set; }
        public int RoleID { get; set; }
        public string Password { get; set; }
        public int? FailedLoginAttempt { get; set; }
        public bool IsPasswordChanged { get; set; }

        public void GetUserCredentials(string strLoginID)
        {
            using (OleDataClient data = new OleDataClient())
            {
                DataTable dt = data.GetDataTable("Select LoginID,EmployeeID,RoleID,PWD,FailedLoginAttempt,IsPasswordChanged from Employee where LoginID='" + strLoginID.ToString().Trim() + "'", "UserCredentials");
                if (dt.Rows.Count > 0)
                {
                    LoginID = dt.Rows[0]["LoginID"].ToString().Trim().ToUpper();
                    EmployeeID = Convert.ToInt32(dt.Rows[0]["EmployeeID"]);
                    RoleID = Convert.ToInt32(dt.Rows[0]["RoleID"]);
                    Password = dt.Rows[0]["PWD"].ToString();

                    if(dt.Rows[0]["FailedLoginAttempt"].Equals(System.DBNull.Value))
                          FailedLoginAttempt = 0;
                    else  FailedLoginAttempt = Convert.ToInt32(dt.Rows[0]["FailedLoginAttempt"]);
                    IsPasswordChanged = Convert.ToBoolean(dt.Rows[0]["IsPasswordChanged"]);
                }

            }
        }
        public bool SaveLastLoginTime(int intEmployeeID)
        {
            using (OleDataClient data = new OleDataClient())
            {
                data.ExecuteNonQuery("Update Employee set LastLoginDate = '" + DateTime.Now + "' where EmployeeID=" + intEmployeeID.ToString());                    
                
            }
            return true;
        }


        public bool UpdatePasswordChangedStatus(int intEmployeeID)
        {
            using (OleDataClient data = new OleDataClient())
            {
                data.ExecuteNonQuery("Update Employee set IsPasswordChanged = 1  where EmployeeID=" + intEmployeeID.ToString());

            }
            return true;
        }


        public bool UpdateFailedLoginAttempt(int intEmployeeID)
        {
            using (OleDataClient data = new OleDataClient())
            {
                int intFailedLoginAttemptCount = 0;
                string strFailedLoginAttempt = data.GetValue("Select FailedLoginAttempt from Employee where EmployeeID=" + intEmployeeID);

                if (strFailedLoginAttempt == null || strFailedLoginAttempt.Length == 0)
                    intFailedLoginAttemptCount = 1;
                else intFailedLoginAttemptCount =   Convert.ToInt32((object)strFailedLoginAttempt) + 1;
                
                //Lock account in case no of Failed attemp is 3
                if (intFailedLoginAttemptCount == 3)
                data.ExecuteNonQuery("Update Employee set FailedLoginAttempt = " + intFailedLoginAttemptCount + ", AccountLocked =  " + 1 + " where EmployeeID=" + intEmployeeID.ToString());

                else data.ExecuteNonQuery("Update Employee set FailedLoginAttempt = " + intFailedLoginAttemptCount + " where EmployeeID=" + intEmployeeID.ToString());
            }
            return true;
        }
    }
}
